//
//  PictureViewVC.swift
//  Basic UI swift
//
//  Created by shmily on 15/7/28.
//  Copyright © 2015年 shmilyAshen. All rights reserved.
//

/*
    注意:要想使旋转平移缩放共同操作,必须设置平移为更改 center(不能设置平移的 transform, 也不能更改 frame)
*/

import UIKit

class PictureViewVC: UIViewController {
 
    //按钮的宽高
    let width = 40
    
    //懒加载显示的按钮
    lazy var btnDisplay : UIButton = {
        let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 180, height: 180))
        btn.setBackgroundImage(UIImage(named: "btn_01"), forState: UIControlState.Normal)
        btn.setTitle("来点我啊", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "btn_02"), forState: UIControlState.Highlighted)
        btn.setTitle("点我干啥", forState: UIControlState.Highlighted)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()

        view.addSubview(self.btnDisplay)

        addBtns()
    }

    ///添加按钮
    func addBtns() {
        let x = [20,60,100,60,240,300,240,300]
        let y = [360,320,360,400,340,340,390,390]
        let normal = ["left_normal","top_normal","right_normal","bottom_normal","plus_normal","minus_normal","plus_normal","minus_normal"]
        let highlighted = ["left_highlighted","top_highlighted","right_highlighted","bottom_highlighted","plus_highlighted","minus_highlighted","plus_highlighted","minus_highlighted"]
        
        for var i = 0; i < 8; i++ {
            let rect = CGRect(x: Int(x[i]), y:Int(y[i]), width: width, height: width)
           let btn = addButton(rect, img1: String(normal[i]), img2: String(highlighted[i]), tag: i)
            view.addSubview(btn)
        }
    }
    
    ///设置按钮的状态
    func addButton(frame:CGRect,img1:String,img2:String,tag:Int) -> UIButton{
        let btn = UIButton(frame: frame)
        btn.setImage(UIImage(named: img1), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: img2), forState: UIControlState.Highlighted)
        btn.tag = tag
        if tag < 4 {
            btn.addTarget(self, action: "moveBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        } else if tag < 6{
            btn.addTarget(self, action: "zoomBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        } else {
            btn.addTarget(self, action: "rotateBtn:", forControlEvents: UIControlEvents.TouchUpInside)
        }
        return btn
    }
    
    ///平移 move
    func moveBtn(btn:UIButton) {
        let x = [-10.0,0.0,10.0,0.0]
        let y = [0.0,-10.0,0.0,10.0]
        var rect = btnDisplay.center
        
        UIView.beginAnimations(nil, context: nil)
        
        rect.x += CGFloat(x[btn.tag])
        rect.y += CGFloat(y[btn.tag])
        btnDisplay.center = rect
        
        UIView.commitAnimations()
    }

    ///缩放按钮
    func zoomBtn(btn:UIButton) {
        UIView.beginAnimations(nil, context: nil)
        if btn.tag == 4 {
            btnDisplay.transform = CGAffineTransformScale(btnDisplay.transform, 1.1,1.1)
        } else {
            btnDisplay.transform = CGAffineTransformScale(btnDisplay.transform, 0.9,0.9)
        }
        UIView.commitAnimations()
    }
    
    ///旋转按钮
    func rotateBtn(btn:UIButton) {
        UIView.beginAnimations(nil, context: nil)
        if btn.tag == 6 {
            btnDisplay.transform = CGAffineTransformRotate(btnDisplay.transform, CGFloat(-M_PI_4))
        } else {
            btnDisplay.transform = CGAffineTransformRotate(btnDisplay.transform, CGFloat(M_PI_4))
        }
        UIView.commitAnimations()
    }
}
